home *** CD-ROM | disk | FTP | other *** search
- Path: telepost.no!usenet
- From: "Alf P. Steinbach" <alfps@telepost.no>
- Newsgroups: comp.lang.c++,comp.os.ms-windows.programmer.misc,comp.os.ms-windows.programmer.tools.misc
- Subject: Re: Using CreateProcess
- Date: Sat, 20 Apr 1996 01:51:02 +0200
- Organization: Telenor Online Public Access
- Message-ID: <317826E6.3F1F@telepost.no>
- References: <31779265.A80@qc.bell.ca>
- NNTP-Posting-Host: monet111.telepost.no
- Mime-Version: 1.0
- Content-Type: text/plain; charset=us-ascii
- Content-Transfer-Encoding: 7bit
- X-Mailer: Mozilla 2.0 (Win95; I)
-
- Eric Marc Loebenberg wrote:
- >
- > Does anyone have an example of using CreateProcess. I am trying to
- > simply use CreateProcess to invoke WORDPAD.EXE but get an Access
- > Violation when trying. Thanks.
-
-
- Sorry about tabs in code below, caused by me being lazy (not fixed
- setup of DevStudio):
-
-
- --------------------------------------------------------------------
- #include <windows.h>
-
-
-
- const char app_title[] = "Run A Program";
-
-
-
- void ErrorBox( char* s )
- {
- MessageBox( 0, s, app_title, MB_ICONEXCLAMATION );
- } // ErrorBox
-
-
-
- void MyYield()
- {
- MSG msg;
-
- PeekMessage( &msg, 0, 0, 0, PM_NOREMOVE );
- } // MyYield
-
-
-
- int WINAPI WinMain(
- HINSTANCE hInstance, // handle to current
- instance
- HINSTANCE hPrevInstance, // handle to previous instance
- LPSTR lpCmdLine, // pointer to command
- line
- int nShowCmd // show state of
- window
- )
- {
- STARTUPINFO startupinfo = {sizeof(STARTUPINFO)};
- PROCESS_INFORMATION processinfo = {0};
- int result = 0;
-
- if (!CreateProcess(
- NULL, // pointer to name of executable
- module
- "C:\\DOC\\MFC-intro\\Test\\mfcrcode\\Release\\mfcrcode.exe",
- // pointer to command line string
- NULL, // pointer to process security
- attributes
- NULL, // pointer to thread security
- attributes
- FALSE, // handle inheritance flag
- 0, // creation flags
- NULL, // pointer to new environment
- block
- NULL, // pointer to current directory
- name
- &startupinfo, // pointer to STARTUPINFO
- &processinfo // pointer to PROCESS_INFORMATION
- ))
- //then
- {
- ErrorBox( "Unable to create process." );
- result = GetLastError();
- }
- else
- { // CreateProcess Ok.
- MyYield(); // Otherwise system is "busy" for a long
- time!
-
- if (WaitForSingleObject( processinfo.hProcess, INFINITE ) !=
- WAIT_OBJECT_0)
- {
- ErrorBox( "Wait completed without process termination."
- );
- result = -1;
- }
- else
- { // WaitForSingleObject Ok.
- unsigned long exit_code; // 32-bit.
-
- if (!GetExitCodeProcess( processinfo.hProcess,
- &exit_code))
- {
- ErrorBox( "Unable to obtain process exit code."
- );
- result = -1;
- }
- else
- { // GetExitCodeProcess Ok.
- char s[256];
- wsprintf( s, "Process exited with code %lu",
- exit_code );
- MessageBox( 0, s, app_title, MB_ICONINFORMATION
- );
-
- if (!CloseHandle( processinfo.hProcess ) )
- { // CloseHandle Ok.
- ErrorBox( "Unable to close process
- handle." );
- } // CloseHandle Ok.
- } // GetExitCodeProcess Ok.
- } // WaitForSingleObject Ok.
- } // CreateProcess Ok.
- return result;
- } // WinMain
- ----------------------------------------------------------------------
-
-
- Hope this helps,
-
-
- - Alf
-